<script>
    $(document).ready(function(){
        $('.isExpired').each(function(){
            $(this).parent().parent().addClass('expired-invoice');
        })
        
        $('.noExpired').each(function(){
            $(this).parent().parent().addClass('no-expired-invoice');
        })
    })
</script>
<style>
    .expired-invoice{
        background-color:red;
    }
    .no-expired-invoice{
        background-color:yellow;
    }

</style>

<?php
$html = '';

foreach ($data as $stateName => $stateData) {
    $header = '<table width="1300" style="" cellspacing="0" cellpadding="2">
            <tbody><tr>
<th colspan="9" align="left" style="background-color: #888888;width:100%;border: 1px solid black;">
  <b style="background-color: #888888;border: 1px double black;"> ' . $stateName . ' </b>  </th> </tr></tbody></table>';

    $body = '';
    $table = new CI_Table();
    $table->set_template(array(
        'table_open' => '<table width="1300" border="1">'
    ));
    $table->add_row(
            array(
        'data' => '<b>State</b>',
        'style' => 'border-bottom: 2px solid  black;'
            ), array(
        'data' => '<b>Contract NO</b>',
        'style' => 'border-bottom: 2px solid  black;'
            ), array(
        'data' => '<b>Type</b>',
        'style' => 'border-bottom: 2px solid  black;'
            ), array(
        'data' => '<b>Date</b>',
        'style' => 'border-bottom: 2px solid  black;',
            ), array(
        'data' => '<b>No</b>',
        'style' => 'border-bottom: 2px solid  black;'
            )
            , array(
        'data' => '<b>County</b>',
        'style' => 'border-bottom: 2px solid  black;',
            ), array(
        'data' => '<b>Custno - Locno - Locname</b>',
        'style' => 'border-bottom: 2px solid  black;'
            ), array(
        'data' => '<b>Earned</b>',
        'align' => 'right',
        'style' => 'border-bottom: 2px solid  black;text-align:right;',
            ), array(
        'data' => '<b>Un-earned</b>',
        'align' => 'right',
        'style' => 'border-bottom: 2px solid  black;text-align:right;'
            )
    );
    //'class'=>$item['isExpired']==true?'expired':null
    foreach ($stateData['items'] as $i => $item) {
        $expiredSign = null;
        if ($item['isExpired'] == true) {
            $expiredSign = '<span class="isExpired"></span>';
        } else {
            $expiredSign = '<span class="noExpired"></span>';
        }
        $table->add_row(
                $item['state'], $item['contractid'] . $expiredSign, ContractType::getText($item['contractype']), "Start Date :" . $item['contractstartdate'], 'End Date :' . $item['contractenddate'], $item['county'], array(
            'data' => $item['custno'] . ' - ' . $item['locno'] . ' - ' . $item['locname'],
                //'colspan'=>4,
                ), array(
            'data' => number_format($item['revenue'], 2),
            'style' => 'text-align:right;'
                ), array(
            'data' => number_format($item['invamt']-$item['revenue'], 2),
            'style' => 'text-align:right;'
                )
        );
        $invoiceList = $item['invoiceList'];
        $invTable = new CI_Table();
        $invTable->set_template(array(
            'table_open' => '<table width="900" border="1">'
        ));
        $invTable->add_row(
                array(
            array(
            'data' => '<b>Type</b>',
            'style' => 'border-bottom: 2px solid  black;'
                ),
                     array(
            'data' => '<b>Invoice No</b>',
            'style' => 'border-bottom: 2px solid  black;',
                ) ,
                 array(
            'data' => '<b>Date</b>',
            'style' => 'border-bottom: 2px solid  black;',
                ) ,
              array(
            'data' => '<b>Amount</b>',
            'align' => 'right',
            'style' => 'border-bottom: 2px solid  black;text-align:right;',
                ),
                    array(
            'data' => '<b>Tax</b>',
            'align' => 'right',
            'style' => 'border-bottom: 2px solid  black;text-align:right;'
                )
               ));
        foreach ($invoiceList as $invoice) {

            /* @var $invoice Invoice */

            $invTable->add_row(
                   $invoice->invoicetype, 
                    $invoice->invoicedate,
                    $invoice->invoice,
                   array(
                'data' => number_format( $invoice->amountcharge, 2),
                'style' => 'text-align:right;'
                    ), array(
                'data' => number_format($invoice->getTotalTax(), 2),
                'style' => 'text-align:right;'
                    )
            );
             
        }
        $table->add_row(
                array(
                    'data'=>'&nbsp'
                ),    
                array(
                    'data'=>$invTable->generate(),
                    'colspan'=>'8'
                )
            );
    }
    $totalTable = new CI_Table();
    $totalTable->set_template(array(
        'table_open' => '<table width="1300" border="1">'
    ));
    $totalTable->add_row(
            array(
        'data' => $stateName,
        'style' => 'width: 705px;border-bottom:  1px solid  black;'
            ), array(
        'data' => 'Earned Revenue',
        'style' => 'width:75px;border-bottom:  1px solid  black;font-style:italic;text-align:right;',
        'aligh' => 'right'
            ), array(
        'data' => 'Un Earned',
        'style' => 'width:75px;border-bottom:  1px solid  black;font-style:italic;text-align:right;',
        'aligh' => 'right'
            )
    );
    $totalTable->add_row(
            array(
        'data' => 'Total'
            ), array(
        'data' => number_format($stateData['revenue'], 2),
        'style' => 'text-align:right;'
            ), array(
        'data' => number_format($stateData['amount']-$stateData['revenue'], 2),
        'style' => 'text-align:right;'
    ));


   
    $html.=$header;
    $html.=$table->generate();
    $html.=$totalTable->generate();
   
    $html.='<br/>';
}
$grandTable = new CI_Table();
$grandTable->set_template(array(
    'table_open' => '<table width="1300" border="1">'
));
$grandTable->add_row(
        array(
    'data' => 'Grand',
    'style' => 'width: 705px;border-bottom:  3px solid  black;font-weight:bold'
        ), array(
    'data' => 'Earned Revenue',
    'style' => 'width:75px;border-bottom:  3px solid  black;font-style:italic;text-align:right;',
    'aligh' => 'right'
        ), array(
    'data' => 'Un Earned ',
    'style' => 'width:75px;border-bottom:  3px solid  black;font-style:italic;text-align:right;',
    'aligh' => 'right'
        )
);
$grandTable->add_row(
        'Total', array(
    'data' => number_format($grandRevenue, 2),
    'style' => 'text-align:right;'
        ), array(
    'data' => number_format($grandAmount-$grandRevenue, 2),
    'style' => 'text-align:right;'
        )
);

$html.=$grandTable->generate();

echo $html;
?>